package com.ddddddl.zhangben.ui.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.ddddddl.zhangben.R;
import com.ddddddl.zhangben.data.Account;
import com.ddddddl.zhangben.data.Type;
import com.ddddddl.zhangben.logic.AccountLogic;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class AccountListAdapter extends BaseAdapter{
	
	private Context mContext;
	private HashMap<Integer, ArrayList<Account>> mAccounts4Show = new HashMap<Integer, ArrayList<Account>>();
	private HashMap<Integer, Double> mAmountSum4Show = new HashMap<Integer, Double>();
	private ArrayList<Account> mAccounts = new ArrayList<Account>();
	
	public static final int NONE_SELECTED = -1;
	private int mSelectedPosition = NONE_SELECTED;
	
	public AccountListAdapter(Context context) {
		mContext = context;
		for (int i = 0; i < 5; i++) {
			mAccounts4Show.put(i, new ArrayList<Account>());
			mAmountSum4Show.put(i, 0d);
		}
	}
	
	@Override
	public int getCount() {
		return mAccounts.size();
	}

	@Override
	public Account getItem(int position) {
		return mAccounts.get(position);
	}

	@Override
	public long getItemId(int position) {
		Account account = mAccounts.get(position);
		if (account != null) {
			
			return account.id;
		} else {
			return 0;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.account_item, null);
		}
		Account account = getItem(position);
		
		TextView divider = (TextView) convertView.findViewById(R.id.divider);
		if (isDividerDate(position)) {
			divider.setVisibility(View.VISIBLE);
			divider.setText(account.date);
		} else {
			divider.setVisibility(View.GONE);
		}
		
		TextView amount = (TextView) convertView.findViewById(R.id.account_amount);
		amount.setText(String.valueOf(account.amount));
		
		TextView firstTypeView = (TextView) convertView.findViewById(R.id.account_first_type);
		Type firstType = AccountLogic.getInstance().getType(account.firstType);
		firstTypeView.setText(firstType.name);
		
		TextView secondTypeView = (TextView) convertView.findViewById(R.id.account_second_type);
		Type secondType = AccountLogic.getInstance().getType(account.secondType);
		if (secondType != null) {
			secondTypeView.setText(secondType.name);
		} else {
			secondTypeView.setText("");
		}
		
		if (position == mSelectedPosition) {
			convertView.findViewById(R.id.toolbar).setVisibility(View.VISIBLE);
			Button editButton = (Button) convertView.findViewById(R.id.edit_account);
			editButton.setOnClickListener((OnClickListener) mContext);
			Button deleteButton = (Button) convertView.findViewById(R.id.delete_account);
			deleteButton.setOnClickListener((OnClickListener) mContext);
		} else {
			convertView.findViewById(R.id.toolbar).setVisibility(View.GONE);
		}
		
		return convertView;
	}

	private boolean isDividerDate(int position) {
		if (position == 0) {
			return true;
		} else if (getItem(position).date.equals(getItem(position - 1).date)) {
			return false;
		}
		return true;
	}

	public void setSelected(int position) {
		if (position == mSelectedPosition) {
			mSelectedPosition = NONE_SELECTED;
		} else {
			mSelectedPosition = position;
		}
	}
	
	public Account getSelectedItem() {
		if (mSelectedPosition == NONE_SELECTED) {
			return null;
		} else {
			return getItem(mSelectedPosition);
		}
	}
	

	public void setData(ArrayList<Account> accounts) {
		mAccounts = accounts;
		notifyDataSetChanged();
	}

	public void addAccount(Account account) {
		mAccounts.add(0, account);
		notifyDataSetChanged();
	}

	public void setSelectedItem(Account account) {
		if (mSelectedPosition != NONE_SELECTED) {
			mAccounts.set(mSelectedPosition, account);
		}
		notifyDataSetChanged();
	}

	public void deleteSelectedItem() {
		if (mSelectedPosition != NONE_SELECTED) {
			mAccounts.remove(mSelectedPosition);
		}
		notifyDataSetChanged();
	}

	public void selectedNothing() {
		setSelected(NONE_SELECTED);
	}

}
